//Copyright (c) 2010 Jimmy Merari - jim.merari@gmail.com

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DotNetNuke.Common;
using System.Text;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;

using DotNetNuke.Security.Roles;

namespace Cinovasi.Scriptagility
{
    public partial class ScriptEditor : System.Web.UI.Page
    {
        protected string AppPath { get { return HttpRuntime.AppDomainAppVirtualPath; } }

        private int m_ModuleId = 0;
        public int ModuleId
        {
            get
            {
                if (m_ModuleId == 0)
                {
                    HttpRequest req = HttpContext.Current.Request;
                    string strMid = (req.QueryString["moduleId"] != null) ? req.QueryString["moduleId"] : "";
                    int.TryParse(strMid, out m_ModuleId);
                }
                return m_ModuleId;
            }
        }

        private int m_TabId;
        public int TabId
        {
            get
            {
                if (m_TabId == 0)
                {
                    HttpRequest req = HttpContext.Current.Request;
                    string strMid = (req.QueryString["TabId"] != null) ? req.QueryString["TabId"] : "";
                    int.TryParse(strMid, out m_TabId);
                }
                return m_TabId;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (VerifyAdmin())
            {
                phMain.Visible = true;
                phWarning.Visible = false;
                hypReturn.NavigateUrl = Globals.NavigateURL(TabId);
                ModuleController mc = new ModuleController();
                ModuleInfo mInfo = mc.GetModule(ModuleId, TabId);
                if(mInfo != null)
                    Page.Header.Title = mInfo.ModuleTitle;
                DataBind();
            }
            else
            {
                phMain.Visible = false;
                phWarning.Visible = true;
            }
        }

        private bool VerifyAdmin()
        {
            PortalSettings ps = PortalController.GetCurrentPortalSettings();
            UserInfo uInfo = UserController.GetCurrentUserInfo();
            if (!uInfo.IsSuperUser && !uInfo.IsInRole(ps.AdministratorRoleName))
                return false;
            else
                return true;
        }

    }
}
